package processingdata.meteor;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.geonames.ToponymSearchCriteria;
import org.geonames.ToponymSearchResult;
import org.geonames.WebService;

public class MeteorTransferData {

	public static void main(String args[]) throws Exception {
		ArrayList<String> files = new ArrayList<String>();
		getFilesInDirectory("project3cs424/meteor_split", files);

		HashMap<String, Double> latitudeMap = new HashMap<String, Double>();
		HashMap<String, Double> longitudeMap = new HashMap<String, Double>();

		for (int i = 0; i < files.size(); i++) {
			File file = new File(files.get(i));
			BufferedReader inputReader = new BufferedReader(new FileReader(
					new File("project3cs424/meteor_split/meteor2")));
			BufferedWriter outputWriter = new BufferedWriter(new FileWriter(
					new File("project3cs424/meteor_final/" + file.getName())));
			while (inputReader.ready()) {
				String inputLine = inputReader.readLine();
				if (inputLine.contains("\t")) {

					System.out.println(inputLine);
					String inputLineParts[] = inputLine.split("\t");
					String place = inputLineParts[inputLineParts.length - 1];

					double latitude;
					double longitude;

					ToponymSearchCriteria searchCriteria = new ToponymSearchCriteria();
					WebService.setUserName("jolly89");

					if (latitudeMap.containsKey(place)
							&& longitudeMap.containsKey(place)) {
						latitude = latitudeMap.get(place);
						longitude = longitudeMap.get(place);
					} else {
						searchCriteria.setQ(place);
						// searchCriteria.setName(city);
						searchCriteria.setCountryCode("US");

						ToponymSearchResult searchResults = WebService
								.search(searchCriteria);

						if (searchResults.getTotalResultsCount() == 0) {
							latitude = 0;
							latitudeMap.put(place, latitude);
							longitude = 0;
							longitudeMap.put(place, longitude);

						} else {

							latitude = searchResults.getToponyms().get(0)
									.getLatitude();
							latitudeMap.put(place, latitude);
							longitude = searchResults.getToponyms().get(0)
									.getLongitude();
							longitudeMap.put(place, longitude);
						}

					}

					outputWriter.write(inputLine + "\t" + latitude + "\t"
							+ longitude + "\n");
				}

			}
			outputWriter.close();
		}

	}

	public static void getFilesInDirectory(String dirName,
			ArrayList<String> files) {
		File dir = new File(dirName);

		// System.out.println(dir);
		if (dir.isDirectory()) {
			if (dir.getName().contains(".svn")) {
				// do nothing
			} else {

				File[] fileArray = dir.listFiles();

				for (int i = 0; i < fileArray.length; i++) {
					// System.out.println(fileArray[i]);
					getFilesInDirectory(fileArray[i].getAbsolutePath(), files);
				}

			}
		} else if (dir.isFile()) {
			files.add(dir.getAbsolutePath());
		}

	}
}
